import {removeTime} from "@/components/page/PhotoPage3/init.ts";

const windowURL = window.URL || window.webkitURL;

export class Img {
    private readonly image: HTMLImageElement
    private readonly loadedProcess: ((arg0: HTMLImageElement) => void)[] | null

    public constructor(data: Blob) {
        this.image = document.createElement('img')
        this.loadedProcess = []

        this.image.src = windowURL.createObjectURL(data)
        this.image.classList.add('backend')
        for (const func of this.loadedProcess) {
            func(this.image)
        }
    }
    // 设置图片加载完成后需要执行的操作
    public onload = (func: (arg0: HTMLImageElement) => void) => {
        this.loadedProcess.push(func)
    }
    // 执行删除图片
    public remove = () => {
        this.image.classList.add('remove')
        setTimeout(() => {
            windowURL.revokeObjectURL(this.image.src)
            this.image.remove()
        }, removeTime)
    }
    // 将图片置顶
    public setFront = () => {
        this.image.classList.remove('backend')
        this.image.classList.add('front')
    }
    // 将图片添加到容器
    public addToContainer = (container: HTMLDivElement) => {
        container.appendChild(this.image)
    }
    // 设置图片移动到右下角
    public setBottom() {
        this.image.classList.add('bottom')
    }
}